﻿using System;
using System.Data;
using System.Xml;
using SyCODE.Component;
using SyCODE.Component.Security;

namespace SyCODE.Blog.Updater
{
    /// <summary>
    /// 模块修改
    /// </summary>
    public class Module : Information
    {
        /// <summary>
        /// 模块修改类的构造函数
        /// </summary>
        /// <param name="bloger"></param>
        public Module(string bloger)
            : base(bloger)
        {
        }

        private bool SafeAuthenticate(string ticket)
        {
            string ticks = Ticket.Decrypt(ticket, Ticket.CONST_CRYPT_KEY, 3);
            if (DateTime.Now.Ticks - Int64.Parse(ticks) < 15000000L)
            {
                return true;
            }
            Function.ClientAlert(StringResource.GetField("safecheckinvited"));
            return false;
        }

        /// <summary>
        /// 设置模块信息
        /// </summary>
        /// <param name="idx">索引位置</param>
        /// <param name="title">标题</param>
        /// <param name="display">显示状态</param>
        /// <param name="position">显示位置</param>
        /// <param name="content">自定义模块的内容</param>
        public void Set(string idx, string title, string display, string position, string content, string ticket)
        {
            XmlElement moduleNode = (XmlElement)UserDocument.DocumentElement.SelectNodes("body/a")[Int32.Parse(idx)];

            bool isCustom = String.Compare(moduleNode.GetAttribute("id"), "custom") == 0;
            if (!isCustom || SafeAuthenticate(ticket))
            {
                moduleNode.SetAttribute("title", title);
                moduleNode.SetAttribute("display", display);
                moduleNode.SetAttribute("position", position);
                if (isCustom)
                {
                    moduleNode.ReplaceChild(UserDocument.CreateCDataSection(Function.DangerTagsFilter(content)), moduleNode.FirstChild);
                }
            }
        }

        /// <summary>
        /// 添加自定义模块
        /// </summary>
        /// <param name="title">标题名称</param>
        /// <param name="display">显示状态</param>
        /// <param name="position">显示位置</param>
        /// <param name="content">自定义模块的内容</param>
        /// <returns>返回添加的结果</returns>
        public bool Add(string title, string display, string position, string content, string ticket)
        {
            if (SafeAuthenticate(ticket))
            {
                XmlElement moduleNode = UserDocument.CreateElement("a");
                moduleNode.SetAttribute("id", "custom");
                moduleNode.SetAttribute("title", title);
                moduleNode.SetAttribute("display", display);
                moduleNode.SetAttribute("position", position);

                XmlNode body = UserDocument.DocumentElement.SelectSingleNode("body");
                body.AppendChild(moduleNode);

                moduleNode.AppendChild(UserDocument.CreateCDataSection(Function.DangerTagsFilter(content)));
                return true;
            }
            return false;
        }

        /// <summary>
        /// 移除字定义模块
        /// </summary>
        /// <param name="idx"></param>
        public void Remove(string idx)
        {
            XmlNode moduleNode = UserDocument.DocumentElement.SelectNodes("body/a")[Int32.Parse(idx)];
            if (String.Compare(moduleNode.Attributes["id"].Value, "custom", true) == 0)
            {
                moduleNode.ParentNode.RemoveChild(moduleNode);
            }
        }

        /// <summary>
        /// 向上移动
        /// </summary>
        /// <param name="idx">模块的索引位置</param>
        public void MoveUp(string idx)
        {
            int index = Int32.Parse(idx);
            if (index > 0)
            {
                XmlNodeList moduleNodes = UserDocument.DocumentElement.SelectNodes("body/a");                
                XmlNode moduleNode = moduleNodes[index];

                moduleNode.ParentNode.InsertBefore(moduleNode, moduleNode.PreviousSibling);
            }         
        }

        /// <summary>
        /// 向下移动
        /// </summary>
        /// <param name="idx">模块的索引位置</param>
        public void MoveDown(string idx)
        {
            int index = Int32.Parse(idx);
            XmlNodeList moduleNodes = UserDocument.DocumentElement.SelectNodes("body/a");
            if (index < moduleNodes.Count - 1)
            {
                XmlNode moduleNode = moduleNodes[index];
                moduleNode.ParentNode.InsertAfter(moduleNode, moduleNode.NextSibling);
            }
        }
    }
}